package com.springboot.dubbo.provider.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import com.springboot.dubbo.provider.api.bean.User;
import com.springboot.dubbo.provider.api.service.UserService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: guodong
 * @Date: 2020/10/15 13:32
 * @Version: 1.0
 * @Description:
 */
@Service(interfaceClass = UserService.class)
//属于Dubbo的@Service注解，非Spring  作用：暴露服务
@Component
public class UserServiceImpl implements UserService {

    private static List<User> userList = new ArrayList<>();

    static {
        User user1 = new User(1L, "aaa");
        User user2 = new User(2L, "bbb");
        User user3 = new User(3L, "ccc");
        User user4 = new User(4L, "ddd");
        User user5 = new User(5L, "eee");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
    }


    @Override
    public User getUserById(Long userId) {

        String name = RpcContext.getContext().getAttachment("name");

        String age = RpcContext.getContext().getAttachment("age");

        System.out.println("name=" + name + ", age= " + age);



        for(User user : userList){
            if(user.getId().equals(userId)){
                return user;
            }
        }
        return null;
    }



}
